﻿using Generic.App.Application.Contracts.Dto;
using Generic.App.Application.Contracts.Service;
using Generic.App.Domain.Entity;
using Generic.Domain.Shared.Exception;
using Generic.Domain.Shared.Const;
using Volo.Abp.Domain.Repositories;

namespace Generic.App.Application.Service
{
    public class AppAccountService : BaseApplicationService<App_Account>, IAppAccountService
    {
        private readonly IRepository<App_Account, string> _appAccountRepository;

        public AppAccountService(IRepository<App_Account, string> appAccountRepository)
        {
            _appAccountRepository = appAccountRepository;
        }

        public async Task<AppAccountDto> LoginAsync(AppAccountLoginDto dto)
        {
            var result = (await _appAccountRepository.GetListAsync(p => p.IsActive == true && p.AppId == dto.AppId && p.AppSecret == dto.AppSecret)).FirstOrDefault();
            if (result != null)
                return ObjectMapper.Map<App_Account, AppAccountDto>(result);
            throw new CustomFriendlyException(nameof(BackErrorConst.LoginError), BackErrorConst.LoginError);
        }
    }
}
